package com.lanmao.emc.autoconfigure;

import com.lanmao.emc.config.UpgradeProperties;
import com.lanmao.emc.utils.UpgradeUtil;
import com.lanmao.emc.enums.UpgradeType;
import com.lanmao.emc.strategy.UpgradeStrategyFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

/**
 * 描述：自动配置类
 *
 * @author lanmao
 * @since 2024年11月11日 20:37
 */
@RequiredArgsConstructor
@ConditionalOnProperty(name = "es.upgrade.enable", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties({UpgradeProperties.class})
@ComponentScan(basePackages = {"com.lanmao.emc", "com.lanmao.ems"})
public class EsManagerAutoConfiguration implements CommandLineRunner {
    @Value("${spring.application.name}")
    private String projectName;

    private final UpgradeProperties upgradeProperties;

    private final UpgradeStrategyFactory upgradeStrategyFactory;

    @Override
    public void run(String... args) {
        UpgradeUtil.projectName=projectName;
        System.out.println();
        System.out.println("  ███████╗███████╗      ███╗   ███╗ █████╗ ███╗   ██╗ █████╗  ██████╗ ███████╗██████╗   ");
        System.out.println("  ██╔════╝██╔════╝      ████╗ ████║██╔══██╗████╗  ██║██╔══██╗██╔════╝ ██╔════╝██╔══██╗  ");
        System.out.println("  █████╗  ███████╗█████╗██╔████╔██║███████║██╔██╗ ██║███████║██║  ███╗█████╗  ██████╔╝  ");
        System.out.println("  ██╔══╝  ╚════██║╚════╝██║╚██╔╝██║██╔══██║██║╚██╗██║██╔══██║██║   ██║██╔══╝  ██╔══██╗  ");
        System.out.println("  ███████╗███████║      ██║ ╚═╝ ██║██║  ██║██║ ╚████║██║  ██║╚██████╔╝███████╗██║  ██║  ");
        System.out.println("  ╚══════╝╚══════╝      ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝╚═╝  ╚═╝ ╚═════╝ ╚══════╝╚═╝  ╚═╝  ");
        System.out.println("        version   :   0.1.0                   ");
        System.out.println();
        UpgradeType upgradeType = UpgradeType.getByValue(upgradeProperties.getStrategy());
        upgradeStrategyFactory.initTable(upgradeType);
        upgradeStrategyFactory.upgradeIndex(upgradeType);
    }

}